package org.vacoor.xqq.ui.comp.trayicon;

import java.awt.*;

/**
 * User: vacoor
 */
public abstract class AppSysTrayIcon {
    private static TrayAnimatedIcon trayIcon;

    private AppSysTrayIcon() {
    }

    public static TrayAnimatedIcon getCurrentAppTrayAnimatedIcon() {
        return trayIcon;
    }

    /**
     * 创建一个托盘图标并返回, 如果不支持或失败返回null
     * 当已经存在一个托盘图标, 则设置属性会返回该对戏那个
     * <p/>
     *
     * @param image
     * @param tooltip
     * @param popup
     * @return
     */
    public static TrayAnimatedIcon addAppTrayAnimatedIcon(Image image, String tooltip, PopupMenu popup) {
        if (trayIcon != null) {
            trayIcon.setImage(image);
            trayIcon.setToolTip(tooltip);
            trayIcon.setPopupMenu(popup);
            return trayIcon;
        }
        if (!SystemTray.isSupported()) {
            return null;
        }

        synchronized (AppSysTrayIcon.class) {
            if (trayIcon == null) {
                trayIcon = new TrayAnimatedIcon(image, tooltip, popup);
                trayIcon.setImageAutoSize(true);

                try {
                    SystemTray.getSystemTray().add(trayIcon);
                } catch (AWTException e) {
                    trayIcon = null;
                }
            }
        }

        trayIcon.setImage(image);
        trayIcon.setToolTip(tooltip);
        trayIcon.setPopupMenu(popup);
        return trayIcon;
    }
}
